﻿namespace _50_C_实现斐波那契数列的三种方法
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //1  1  2  3  5  8


            Console.ReadKey();
        }

        public static int Fib1(int n)
        {
            if (n <= 0)
            {
                return 0;
            }

            if (n <= 2)
            {
                return 1;
            }
            return Fib1(n - 1) + Fib1(n - 2);
        }

        public static int Fib2(int n)
        {
            if (n <= 0)
            {
                return 0;
            }

            if (n <= 2)
            {
                return 1;
            }
            int[] num = new int[n];
            num[0] = 1;
            num[1] = 0;
            for (int i = 2; i < n; i++)
            {
                num[i] = num[i - 1] + num[i - 2];
            }
            return num[n];
        }

        public static int Fib3(int n)
        {
            if (n <= 0)
            {
                return 0;
            }

            if (n <= 2)
            {
                return 1;
            }
            int ret = 0;
            int frist = 1;
            int sencond = 1;
            for (int i = 2; i < n; i++)
            {
                ret = frist + sencond;
                frist = sencond;
                sencond = ret;
            }
            return ret;
        }
    }
}
